<template>
    <div id="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader @addTodoEvent="addTodo"></MyHeader>
                <MyList :todos="todos"></MyList>
                <MyFooter :todos="todos" @checkAllTodoEvent="checkAllTodo" @clearCheckedTodoEvent="clearCheckedTodo"></MyFooter>
            </div>
        </div>
    </div>
</template>

<script>
// 现阶段学习到的组件通信：
// 父组件给子组件传递使用props，
// 子组件给父组件传递，需要父组件先给子组件用props传递一个接收函数，子组件可以利用这个函数给父组件传递信息
import MyHeader from "@/components/MyHeader.vue";
import MyList from "@/components/MyList.vue";
import MyFooter from "@/components/MyFooter.vue";

export default {
    name: "App",
    components: {MyHeader, MyList, MyFooter},
    data() {
        return {
            todos: JSON.parse(localStorage.getItem('todos')) || [],
        }
    },
    methods: {
        addTodo(todo){
            // 将这个方法交给Header子组件，子组件调用方法，此方法接收Header组件传过来的数据
            console.log("App收到：",todo)
            this.todos.unshift(todo);
        },
        checkTodo(id){
            this.todos.forEach((todo) => {
                if (todo.id === id) todo.done = !todo.done
            })
        },
        delTodo(id){
            this.todos = this.todos.filter((todo)=>{
                return todo.id !== id;
            })
        },
        updateTodo(id,title) {
            this.todos.forEach((todo) => {
                if (todo.id === id) todo.title = title;
            })
        },
        checkAllTodo(done){
            this.todos.forEach((todo) => {
                todo.done = done
            })
        },
        clearCheckedTodo(){
            this.todos = this.todos.filter((todo) => {
                return todo.done === false;
            })
        }
    },
    watch: {
        todos: {
            deep:true,
            handler(value) {
                localStorage.setItem('todos',JSON.stringify(value))
            }
        }
    },
    mounted() {
        this.$bus.$on('checkTodo',this.checkTodo)
        this.$bus.$on('delTodo',this.delTodo)
        this.$bus.$on('updateTodo',this.updateTodo)
    },
    beforeDestroy() {
        this.$bus.$off(['checkTodo','delTodo','updateTodo'])
    }
}
</script>

<style>
/*base*/
body {
    background: #fff;
}

.btn {
    display: inline-block;
    padding: 4px 12px;
    margin-bottom: 0;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
    border-radius: 4px;
}

.btn-danger {
    color: #fff;
    background-color: #da4f49;
    border: 1px solid #bd362f;
}

.btn-danger:hover {
    color: #fff;
    background-color: #bd362f;
}

.btn-edit {
    color: #fff;
    background-color: skyblue;
    border: 1px solid #358faf;
    margin-right: 5px;
}

.btn-edit:hover {
    color: #fff;
    background-color: #197b9d;
}

.btn:focus {
    outline: none;
}

.todo-container {
    width: 600px;
    margin: 0 auto;
}

.todo-container .todo-wrap {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
}

</style>